/*
 * Adiciona voto
 * */
package servlets;

import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.Comentario;
import beans.Voto;
import dbclass.ComentarioDAO;
import dbclass.ComentarioJDBC;
import dbclass.VotoDAO;
import dbclass.VotoJDBC;

/**
 * Servlet implementation class VotoAdd
 */
public class VotoAdd extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VotoAdd() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		
		VotoDAO votoDAO = new VotoJDBC();
		String ip = request.getLocalAddr();
		int nota = Integer.parseInt(request.getParameter("nota"));
		long id_noticia = (long)Integer.parseInt(request.getParameter("id_noticia"));
		 
		Voto voto = new Voto(0, ip, nota, id_noticia);  
		
		try { 
				  
			votoDAO.insert(voto); 
			String id_n = id_noticia + "";
			String url="NoticiasVer?msg_sucesso=Nota adicionada&id=" + id_n;   
 
			response.sendRedirect(url); 
		} catch (ClassNotFoundException e) {  
			// TODO Auto-generated catch block 
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
